using Admin2022Soft2.Api.Service;
using Admin2022Soft2.Application.Common.Interface;
using Admin2022Soft2.Infrastructrue;
using Admin2022Soft2.Infrastructrue.Filters;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(
        policy =>
        {
            policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
        });
});

// Add services to the container.
// 注册http请求上下文
builder.Services.AddHttpContextAccessor();
// 注册当前用户信息服务
builder.Services.AddTransient(typeof(ISessionUserService),typeof(SessionUserService));

builder.Services.AddControllers(options=>{
    options.Filters.Add(typeof(AuditLogActionFilter));
    options.Filters.Add(typeof(HttpGlobalExceptionFilter));
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddInfrastructure(builder.Configuration);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseCors();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

// 调用扩展方法
app.MigrateDatabase();

app.Run();
